import os
from conans import ConanFile, CMake, tools
from conans import tools
import os
import sys
import sys
import os
import shutil

sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path


class IniParserConan(ConanFile):
    name = "iniparser"
    version = "2.1.2"
    url = "https://gitee.com/mirrors/iniparser.git"
    description = "parse ini file"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"

    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)

        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            print("source  cwd is", os.getcwd())
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url)
    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)
    def build(self):
        try:
            os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
            pass
        except  :
            pass
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make")
                os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除



    def package(self):
        shutil.copytree(self.build_folder + '\\pkgsrc\\src\\', self.package_folder + "\\include\\iniparser")
        self.copy("pkgsrc\\*.a", dst="lib", keep_path=False)
        
    def package_info(self):
        self.cpp_info.name = 'iniparser'
        self.cpp_info.libs = ['libiniparser.a']
        self.cpp_info.names["cmake_find_package"] = "iniparser"
        self.cpp_info.names["cmake_find_package_multi"] = "iniparser"

        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "iniparser"
                self.cpp_info.names["cmake_find_package_multi"] = "iniparser"
                self.cpp_info.libdirs = ['pkgsrc/lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libevent_c']
                self.cpp_info.buildirs = 'pkgsrc'
